<?php
namespace Hr\OnlineBundle\Entity;

use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\EqualTo;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\Common\Collections\ArrayCollection; 
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Hr\OnlineBundle\Lib\Globals;


/**
 * @ORM\Entity(repositoryClass="Hr\OnlineBundle\Entity\Repository\UserRepository")
 * @ORM\Table(name="user")
 * @ORM\HasLifecycleCallbacks()
 */
class User implements  UserInterface, \Serializable
{
	/**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(name="first_name", type="string", length=100)
     */
    protected $firstName;

    /**
     * @ORM\Column(name="last_name", type="string", length=100)
     */
    protected $lastName;

    /**
     * @ORM\Column(type="string", length=150, unique=true, nullable=false)
     */
    protected $email;



    /**
     * @ORM\Column(name="gender", type="string", length=1)
     */
    protected $gender;


   /**
     * @ORM\Column(type="string", length=32)
     */
    private $salt;


    /**
     * @ORM\Column(type="string", length=100)
     */
    protected $password;

    /**
     * we can use an annotation heer such as @ Equal To ("password", message="Passwords do not match!")
	 * see also http://www.yewchube.com/2011/08/symfony-2-field-comparison-validator/
     */
    protected $repeatPassword;

    protected $oldPassword;


    /**
     * @ORM\Column(name="date_of_birth", type="date", nullable=true)
     */
    protected $dateOfBirth;



    /**
     * @ORM\Column(name="phone_mobile", type="string", length=20, nullable=true)
     */
    protected $phoneMobile;

	/**
     * @ORM\Column(name="phone_landline", type="string", length=20, nullable=true)
     */
    protected $phoneLandline;

	
	/**
     * @ORM\Column(name="country", type="string", length=3, nullable=true)
     */
    protected $country;

	/**
     * @ORM\Column(name="postcode", type="integer", length=6, nullable=true)
     */
    protected $postcode;

	/**
     * @ORM\Column(name="city", type="string", length=100, nullable=true)
     */
    protected $city;

	/**
     * @ORM\Column(name="`address_line_1", type="string", length=20, nullable=true)
     */
    protected $addressLine1;

	/**
     * @ORM\Column(name="`address_line_2", type="string", length=20, nullable=true)
     */
    protected $addressLine2;

    /**
     * @ORM\Column(name="created_at", type="datetime", nullable=true)
     */
    protected $createdAt;

    /**
     * @ORM\Column(name="updated_at", type="datetime", nullable=true)
     */
    protected $updatedAt;


    /**
     * @ORM\Column(name="is_email_verified", type="boolean")
     */
    protected $isEmailVerified;


    /**
     * @ORM\Column(name="is_active", type="boolean")
     */
    private $isActive;


    /**
     * @ORM\Column(name="earliest_start_date", type="date", nullable=true)
     */
    private $earliestStartDate;

    /**
     * @ORM\Column(name="notice_period", type="string", length=30,  nullable=true)
     */
    private $noticePeriod;

    /**
     * @ORM\Column(name="min_salary", type="string", length=30,  nullable=true)
     */
    private $minSalary;

    /**
     * @ORM\Column(name="objectives", type="text", nullable=true)
     */
    private $objectives;

    /**
     * @ORM\Column(name="willingness_to_travel", type="boolean",  nullable=true)
     */
    private $willingnessToTravel;

   /**
     * @ORM\Column(name="relocation_notes", type="text", nullable=true)
     */
    private $relocationNotes;

    /**
     * @ORM\Column(name="driving_license", type="string", length=50,  nullable=true)
     */
    private $drivingLicense;


    private $cv;
    private $letter;
    private $reference;

	/**
     * @ORM\OneToMany(targetEntity="Document", mappedBy="user")
     */
    protected $documents;


    public function __construct()
    {
        $this->setCreatedAt(new \DateTime());
        $this->setUpdatedAt(new \DateTime());
        $this->setIsEmailVerified(false);

		$this->isActive = true; // todo consider changing
        $this->salt = md5(uniqid(null, true));

		$this->documents = new ArrayCollection();

    }

    /**
     * Get documents
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getDocuments()
    {
        return $this->documents;
    }



    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }

    public function getPassword()
    {
        return $this->password;
    }

    public function setPassword($password)
    {
        $this->password = $password;
    }


    public function getRepeatPassword()
    {
        return $this->repeatPassword;
    }

    public function getOldPassword()
    {
        return $this->oldPassword;
    }

    public function setOldPassword($oldPassword)
    {
        $this->oldPassword = $oldPassword;
    }


    public function setRepeatPassword($repeatPassword)
    {
        $this->repeatPassword = $repeatPassword;
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set created
     *
     * @param \DateTime $createdAt
     * @return User
     */
    public function setCreatedAt($createdAt)
    {
        $this->createdAt = $createdAt;
    
        return $this;
    }

    /**
     * Get createdAt
     *
     * @return \DateTime 
     */
    public function getCreatedAt()
    {
        return $this->createdAt;
    }

    /**
     * Set updatedAt
     *
     * @param \DateTime $updatedAt
     * @return User
     */
    public function setUpdatedAt($updatedAt)
    {
        $this->updatedAt = $updatedAt;
    
        return $this;
    }

    /**
     * Get updatedAt
     *
     * @return \DateTime 
     */
    public function getUpdatedAt()
    {
        return $this->updatedAt;
    }



	public static function loadValidatorMetadata(ClassMetadata $metadata)
    {
        $metadata->addPropertyConstraint('firstName', new NotBlank());
        $metadata->addPropertyConstraint('lastName', new NotBlank());

        $metadata->addPropertyConstraint('email', new Email(array(
			'message' => 'Please enter a valid email!'
		)));

        $metadata->addPropertyConstraint('postcode', new Length(array('min'=> 5, 'max'=> 6)));

        $metadata->addPropertyConstraint('password', new NotBlank());

		$minPasswordLength = Globals::getMinPasswordLength();
		$metadata->addPropertyConstraint('password', new Length(array('min'=> $minPasswordLength, 'max'=> 100)));

    }


    /**
     * Set firstName
     *
     * @param string $firstName
     * @return User
     */
    public function setFirstName($firstName)
    {
        $this->firstName = $firstName;
    
        return $this;
    }

    /**
     * Get firstName
     *
     * @return string 
     */
    public function getFirstName()
    {
        return $this->firstName;
    }

    /**
     * Set lastName
     *
     * @param string $lastName
     * @return User
     */
    public function setLastName($lastName)
    {
        $this->lastName = $lastName;
    
        return $this;
    }

    /**
     * Get lastName
     *
     * @return string 
     */
    public function getLastName()
    {
        return $this->lastName;
    }




	public function getName()
    {
        return $this->firstName.' '.$this->lastName;
    }



    /**
     * Set isEmailVerified
     *
     * @param boolean $isEmailVerified
     * @return User
     */
    public function setIsEmailVerified($isEmailVerified)
    {
        $this->isEmailVerified = $isEmailVerified;
    
        return $this;
    }

    /**
     * Get isEmailVerified
     *
     * @return boolean 
     */
    public function getIsEmailVerified()
    {
        return $this->isEmailVerified;
    }

    /**
     * Set dateOfBirth
     *
     * @param \DateTime $dateOfBirth
     * @return User
     */
    public function setDateOfBirth($dateOfBirth)
    {
        $this->dateOfBirth = $dateOfBirth;
    
        return $this;
    }

    /**
     * Get dateOfBirth
     *
     * @return \DateTime 
     */
    public function getDateOfBirth()
    {
        return $this->dateOfBirth;
    }

    /**
     * Set phoneMobile
     *
     * @param string $phoneMobile
     * @return User
     */
    public function setPhoneMobile($phoneMobile)
    {
        $this->phoneMobile = $phoneMobile;
    
        return $this;
    }

    /**
     * Get phoneMobile
     *
     * @return string 
     */
    public function getPhoneMobile()
    {
        return $this->phoneMobile;
    }

    /**
     * Set phoneLandline
     *
     * @param string $phoneLandline
     * @return User
     */
    public function setPhoneLandline($phoneLandline)
    {
        $this->phoneLandline = $phoneLandline;
    
        return $this;
    }

    /**
     * Get phoneLandline
     *
     * @return string 
     */
    public function getPhoneLandline()
    {
        return $this->phoneLandline;
    }

    /**
     * Set country
     *
     * @param string $country
     * @return User
     */
    public function setCountry($country)
    {
        $this->country = $country;
    
        return $this;
    }

    /**
     * Get country
     *
     * @return string 
     */
    public function getCountry()
    {
        return $this->country;
    }

    /**
     * Set postcode
     *
     * @param integer $postcode
     * @return User
     */
    public function setPostcode($postcode)
    {
        $this->postcode = $postcode;
    
        return $this;
    }

    /**
     * Get postcode
     *
     * @return integer 
     */
    public function getPostcode()
    {
        return $this->postcode;
    }

    /**
     * Set city
     *
     * @param string $city
     * @return User
     */
    public function setCity($city)
    {
        $this->city = $city;
    
        return $this;
    }

    /**
     * Get city
     *
     * @return string 
     */
    public function getCity()
    {
        return $this->city;
    }

    /**
     * Set addressLine1
     *
     * @param string $addressLine1
     * @return User
     */
    public function setAddressLine1($addressLine1)
    {
        $this->addressLine1 = $addressLine1;
    
        return $this;
    }

    /**
     * Get addressLine1
     *
     * @return string 
     */
    public function getAddressLine1()
    {
        return $this->addressLine1;
    }


    /**
     * Set addressLine2
     *
     * @param string $addressLine2
     * @return User
     */
    public function setAddressLine2($addressLine2)
    {
        $this->addressLine2 = $addressLine2;
    
        return $this;
    }

    /**
     * Get addressLine2
     *
     * @return string 
     */
    public function getAddressLine2()
    {
        return $this->addressLine2;
    }

    /**
     * Set gender
     *
     * @param string $gender
     * @return User
     */
    public function setGender($gender)
    {
        $this->gender = $gender;
    
        return $this;
    }

    /**
     * Get gender
     *
     * @return string 
     */
    public function getGender()
    {
        return $this->gender;
    }

 /**
     * @inheritDoc
     */
    public function getSalt()
    {
        return $this->salt;
    }


	/**
     * @inheritDoc
     */
    public function getRoles()
    {
        return array('ROLE_USER');
    }



   /**
     * @inheritDoc
     */
    public function eraseCredentials()
    {
    }

    /**
     * @see \Serializable::serialize()
     */
    public function serialize()
    {
        return serialize(array(
            $this->id,
        ));
    }

    /**
     * @see \Serializable::unserialize()
     */
    public function unserialize($serialized)
    {
        list (
            $this->id,
        ) = unserialize($serialized);
    }

	public function isAccountNonExpired()
    {
        return true;
    }

    public function isAccountNonLocked()
    {
        return true;
    }

    public function isCredentialsNonExpired()
    {
        return true;
    }

    public function isEnabled()
    {
        return $this->isActive;
    }

    public function getUsername()
    {
        return $this->email;
    }




    /**
     * Set isActive
     *
     * @param boolean $isActive
     * @return User
     */
    public function setIsActive($isActive)
    {
        $this->isActive = $isActive;
    
        return $this;
    }

    /**
     * Get isActive
     *
     * @return boolean 
     */
    public function getIsActive()
    {
        return $this->isActive && $this->isEmailVerified;
    }



	/**
     * Sets cv.
     *
     * @param UploadedFile $cv
     */
    public function setCV(UploadedFile $cv = null)
    {
        $this->cv = $cv;
    }

    /**
     * Get file.
     *
     * @return UploadedFile
     */
    public function getCv()
    {
        return $this->cv;
    }



	/**
     * Sets letter.
     *
     * @param UploadedFile $letter
     */
    public function setLetter(UploadedFile $letter = null)
    {
        $this->letter = $letter;
    }

    /**
     * Get letter.
     *
     * @return UploadedFile
     */
    public function getLetter()
    {
        return $this->letter;
    }



	/**
     * Sets reference.
     *
     * @param UploadedFile $reference
     */
    public function setReference(UploadedFile $reference = null)
    {
        $this->reference = $reference;
    }

    /**
     * Get reference.
     *
     * @return UploadedFile
     */
    public function getReference()
    {
        return $this->reference;
    }



    /**
     * Set salt
     *
     * @param string $salt
     * @return User
     */
    public function setSalt($salt)
    {
        $this->salt = $salt;
    
        return $this;
    }
    /**
     * Set earliestStartDate
     *
     * @param \DateTime $earliestStartDate
     * @return User
     */
    public function setEarliestStartDate($earliestStartDate)
    {
        $this->earliestStartDate = $earliestStartDate;
   
        return $this;
    }

    /**
     * Get earliestStartDate
     *
     * @return \DateTime
     */
    public function getEarliestStartDate()
    {
        return $this->earliestStartDate;
    }

    /**
     * Set noticePeriod
     *
     * @param string $noticePeriod
     * @return User
     */
    public function setNoticePeriod($noticePeriod)
    {
        $this->noticePeriod = $noticePeriod;
   
        return $this;
    }

    /**
     * Get noticePeriod
     *
     * @return string
     */
    public function getNoticePeriod()
    {
        return $this->noticePeriod;
    }

    /**
     * Set minSalary
     *
     * @param string $minSalary
     * @return User
     */
    public function setMinSalary($minSalary)
    {
        $this->minSalary = $minSalary;
   
        return $this;
    }

    /**
     * Get minSalary
     *
     * @return string
     */
    public function getMinSalary()
    {
        return $this->minSalary;
    }

    /**
     * Set objectives
     *
     * @param string $objectives
     * @return User
     */
    public function setObjectives($objectives)
    {
        $this->objectives = $objectives;
   
        return $this;
    }

    /**
     * Get objectives
     *
     * @return string
     */
    public function getObjectives()
    {
        return $this->objectives;
    }

    /**
     * Set willingnessToTravel
     *
     * @param boolean $willingnessToTravel
     * @return User
     */
    public function setWillingnessToTravel($willingnessToTravel)
    {
        $this->willingnessToTravel = $willingnessToTravel;
   
        return $this;
    }

    /**
     * Get willingnessToTravel
     *
     * @return boolean
     */
    public function getWillingnessToTravel()
    {
        return $this->willingnessToTravel;
    }

    /**
     * Set relocationNotes
     *
     * @param string $relocationNotes
     * @return User
     */
    public function setRelocationNotes($relocationNotes)
    {
        $this->relocationNotes = $relocationNotes;
   
        return $this;
    }

    /**
     * Get relocationNotes
     *
     * @return string
     */
    public function getRelocationNotes()
    {
        return $this->relocationNotes;
    }

    /**
     * Set drivingLicense
     *
     * @param string $drivingLicense
     * @return User
     */
    public function setDrivingLicense($drivingLicense)
    {
        $this->drivingLicense = $drivingLicense;
   
        return $this;
    }

    /**
     * Get drivingLicense
     *
     * @return string
     */
    public function getDrivingLicense()
    {
        return $this->drivingLicense;
    }
}